package controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.JComboBox;

import model.CostumerModel;
import model.Employee;
import model.Envelope;
import client.CcrmApp;
import view.CostumerRelEmpGui;

/**
 * This Class is a Costumer relation Employee Controller, which extends AbstractTransfer
 * @author jacob
 *
 */
public class CostumerRelEmpCont extends  AbstractTransfer {
	private CostumerRelEmpGui costReView;
	private LoginController prevController;
	private CostumerModel newCostumer;
	private CostumerRelEmpCont CurrCon;
	private String AreaSelected = null;
	private Employee user1;

/**
 * 
 * @param costReview is Gui of Costumer relation Employee  instance
 * @param loginController is previous controller instance  
 * @param user is Employee instance
 */

	public CostumerRelEmpCont(CostumerRelEmpGui costReview,LoginController loginController,Employee user) {
		// TODO Auto-generated constructor stub
	//	super(costReview,user);
		user1 = user;
		this.costReView = costReview;
		this.prevController= loginController;
		CurrCon = this;
		costReView.addRegisterActionListener(new RegisterListener());
		costReView.addbackBActionListener(new back1Listener());
		costReView.addAreaActionListener(new AreaItemListener());
		initArea();
	}
	
	/**
	 * Inner class that handles when Button back Pressed, implements ActiontListener
	 * @author jacob
	 *
	 */
	
	class back1Listener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			//dispose the current Gui and show the previous Gui 
             UpdateDB11();  //update the 
			costReView.dispose();
			prevController.getLoginG().ClearText();
			prevController.getLoginG().setVisible(true);
		}
	
	}
	

	private void initArea() { //send to server to get list of area
		ArrayList<String> area = new ArrayList<String>();
		Envelope ev = new Envelope(area, "get area");
		CcrmApp.clien.setCurrObj(CurrCon);
		sendToServer(ev);
	}
	
	private boolean checkInput() {
		// TODO Auto-generated method stub
		//check if the input is correct
		if(costReView.getTextAge() == 0 || costReView.getTextAgeTe().equals("") || costReView.getTextEmail().equals("") || costReView.getTextId().equals("")  
				|| costReView.getTextLastName().equals("") || costReView.getTextphone().equals("") || costReView.getTextUserName().equals("")
				|| AreaSelected == null){	  
		      costReView.setWarningMessageVisibleTrue("You most fill all the filds");
			   costReView.ClearText();
		        return false;
		}
		
		else {
			if(costReView.getTextAge() < 0) {
				 costReView.setWarningMessageVisibleTrue("Illigal input of age");
				   costReView.ClearText();
			        return false;
			}
		}
		
		return true;
	}	
	
/**
 *  Inner class that handles when Button Register Pressed, implements ActiontListener
 * @author jacob
 *
 */

	class RegisterListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// check if the input is correctly and send to server to get the the information 
			if(checkInput()) {
				Envelope ev1 = new Envelope(new CostumerModel(costReView.getTextId(), costReView.getTextLastName(), costReView.getTextUserName(),costReView.getTextphone(), 
						costReView.getTextEmail(), AreaSelected, costReView.getTextAge()),"Serch costumer");
				CcrmApp.clien.setCurrObj(getCurrCon()); //set the client to this controller to return the messege
				sendToServer(ev1);
				
			}
				
		}

	}
	/**
	 *  Inner class that handles when ComboBox is  Pressed, implements ItemListener
	 * @author jacob
	 *
	 */
	class AreaItemListener implements ItemListener{

		@Override
		public void itemStateChanged(ItemEvent e) {
			// save the data you select in AreaSelected 
			JComboBox Combo = getCostReView().getAreaC();
			AreaSelected = (String)Combo.getSelectedItem();
		}
		
	}
	

	 @SuppressWarnings("unchecked")
	 @Override
	 /*
	  * (non-Javadoc)
	  * @see controllers.AbstractTransfer#handleDBResult(java.lang.Object)
	  */
	public void handleDBResult(Object message) {
		 Envelope mes = (Envelope)message;
		 //set the area to the Combo box
	   if(mes.getTask().equals("get area")) {
			for(int i = 0; i < ((ArrayList<String>)mes.getObject()).size() ; i++)
				costReView.getAreaC().addItem(((ArrayList<String>)mes.getObject()).get(i));
	   }
	   // set Warning message on Gui
	   if(mes.getTask().equals("Serch costumer")){
		   if(mes.getMess().equals("id exist"))
			   costReView.setWarningMessageVisibleTrue("This Id of Costumer is exist");
		   if(mes.getMess().equals("email exist"))
			   costReView.setWarningMessageVisibleTrue("This email of Costumer is exist");
		   if(mes.getMess().equals("id and email exist"))
			   costReView.setWarningMessageVisibleTrue("This Id and email of Costumer is exist");
		   if(mes.getMess().equals("Insert Costumer ok"))
			   costReView.setWarningMessageVisibleTrue("Subscribe success");
		   costReView.ClearText(); 
	   }
	 }
	 

/**
 * get customer Gui 
 * @return CostumerRelEmpGui
 */
	
	public CostumerRelEmpGui getCostReView() {
		return costReView;
	}
/**
 * set customer Gui 
 * @param costReView
 */
	public void setCostReView(CostumerRelEmpGui costReView) {
		this.costReView = costReView;
	}
/**
 * get prev Controller
 * @return LoginController
 */
	public LoginController getPrevController() {
		return prevController;
	}
/**
 * set prev Controller
 * @param prevController
 */
	public void setPrevController(LoginController prevController) {
		this.prevController = prevController;
	}
	/**
	 * get Costumer Model (entity)
	 * @return CostumerModel
	 */
	public CostumerModel getNewCostumer() {
		return newCostumer;
	}
/**
 * set New Customer
 * @param newCostumer
 */
	public void setNewCostumer(CostumerModel newCostumer) {
		this.newCostumer = newCostumer;
	}
	/**
	 * get Current  Contoroller
	 * @return CostumerRelEmpCont
	 */
	public CostumerRelEmpCont getCurrCon() {
		return CurrCon;
	}
	/**
	 * UpdateDB11 is function that Update status to the current user to 0(logoff)
	 */
	 public void UpdateDB11(){
		 Envelope en1 = new Envelope(user1,"updateStatus0");
		 sendToServer(en1);
	 }

}
